package stream;


import reference.Student;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiuYan
 * @date 2021/11/19 13:10
 */
public class Part11 {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(
                new Student(20, "lisi"),
                new Student(10, "zhangsan"),
                new Student(30, "wangwu"),
                new Student(20, "lisi1"),
                new Student(10, "zhangsan1"),
                new Student(30, "wangwu1"),
                new Student(20, "lisi2"),
                new Student(10, "zhangsan2"),
                new Student(30, "wangwu2"),
                new Student(40, "goudan"),
                new Student(20, "lisi3"),
                new Student(10, "zhangsan4"),
                new Student(30, "wangwu5"),
                new Student(20, "lisi6"),
                new Student(10, "zhangsan7"),
                new Student(30, "wangwu8"),
                new Student(20, "lisi9"),
                new Student(10, "zhangsan22"),
                new Student(30, "wangwu23"),
                new Student(40, "goudan333"));

        // 分组
        Map<Integer, List<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
        System.out.println(collect);
        // 多级分组
        Map<String, Map<String, List<Student>>> collect1 = studentList.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(e -> {
                    if (e.getAge() < 20) {
                        return "青年";
                    } else if (e.getAge() < 30) {
                        return "中年";
                    } else {
                        return "老年组";
                    }
                }
        )));

        System.out.println(collect1);

        // 分区
        Map<Boolean, List<Student>> collect2 = studentList.stream().collect(Collectors.partitioningBy(student -> student.getAge() > 0));
        System.out.println(collect2);

        // 收集
        String collect3 = studentList.stream().map(Student::getName).collect(Collectors.joining(","));
        System.out.println(collect3);


    }
}